//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package xml;

import database.Util;
import entity.Column;
import entity.Table;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import templet.Utils;
import util.Unicode;

public class DatabaseXml {
    public DatabaseXml() {
    }
    
    public static void writeDatabaseXml(Util util, Map<String, String> propertyMap, String outPath) throws ClassNotFoundException, SQLException {
        Document doc = DocumentHelper.createDocument();
        doc.setXMLEncoding("utf-8");
        Element root = doc.addElement("db");
        root.addAttribute("name", util.getDbName());
        root.addAttribute("driverName", util.getDriverName());
        root.addAttribute("userName", util.getUserName());
        root.addAttribute("passWord", util.getPassWord());
        root.addAttribute("url", util.getUrl());
        Iterator var6 = propertyMap.keySet().iterator();
        
        while(var6.hasNext()) {
            String key = (String)var6.next();
            Element element = root.addElement("property");
            element.addAttribute("name", key);
            element.setText((String)propertyMap.get(key));
        }
        
        List<Table> tableList = util.getDbInfo();
        Iterator var15 = tableList.iterator();
        
        while(true) {
            int keycount;
            Table table;
            do {
                if (!var15.hasNext()) {
                    writeXml(outPath, doc);
                    return;
                }
                
                table = (Table)var15.next();
                keycount = 0;
                Iterator var10 = table.getColumns().iterator();
                
                while(var10.hasNext()) {
                    Column column = (Column)var10.next();
                    if (column.getColumnKey().equals("PRI")) {
                        ++keycount;
                    }
                }
            } while(keycount != 1);
            
            Element tableElement = root.addElement("table");
            tableElement.addAttribute("name", table.getName());
            tableElement.addAttribute("name2", Utils.getTableName2(table.getName()));
            tableElement.addAttribute("comment", Unicode.toUnicodeString(table.getComment()));
            tableElement.addAttribute("key", table.getKey());
            Iterator var11 = table.getColumns().iterator();
            
            while(var11.hasNext()) {
                Column column = (Column)var11.next();
                Element columnElement = tableElement.addElement("column");
                columnElement.addAttribute("name", column.getColumnName());
                columnElement.addAttribute("name2", Utils.getColumnName2(column.getColumnName()));
                columnElement.addAttribute("type", column.getColumnType());
                columnElement.addAttribute("dbtype", column.getColumnDbType());
                columnElement.addAttribute("comment", Unicode.toUnicodeString(column.getColumnComment()));
                columnElement.addAttribute("key", column.getColumnKey());
                columnElement.addAttribute("decimal_digits", String.valueOf(column.getDecimal_digits()));
                columnElement.addAttribute("colums_size", String.valueOf(column.getColums_size()));
                if (column.getColumnKey().equals("PRI")) {
                    ++keycount;
                }
            }
        }
    }
    
    public static List<Table> readDatabaseXml(String xmlPath) {
        ArrayList list = new ArrayList();
        
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(xmlPath + "\\db.xml");
            Element dbe = doc.getRootElement();
            List<Element> elist = dbe.elements();
            Iterator var7 = elist.iterator();
            
            while(true) {
                Element e;
                do {
                    if (!var7.hasNext()) {
                        return list;
                    }
                    
                    e = (Element)var7.next();
                } while(!e.getName().equals("table"));
                
                Table table = new Table();
                table.setName(e.attributeValue("name"));
                table.setName2(e.attributeValue("name2"));
                table.setComment(Unicode.decodeUnicode(e.attributeValue("comment")));
                table.setKey(e.attributeValue("key"));
                List<Column> columns = new ArrayList();
                List<Element> elist2 = e.elements();
                Iterator var12 = elist2.iterator();
                
                while(var12.hasNext()) {
                    Element e2 = (Element)var12.next();
                    Column column = new Column();
                    column.setColumnName(e2.attributeValue("name"));
                    column.setColumnName2(e2.attributeValue("name2"));
                    column.setColumnType(e2.attributeValue("type"));
                    column.setColumnDbType(e2.attributeValue("dbtype"));
                    column.setColumnComment(Unicode.decodeUnicode(e2.attributeValue("comment")));
                    column.setColumnKey(e2.attributeValue("key"));
                    columns.add(column);
                }
                
                table.setColumns(columns);
                list.add(table);
            }
        } catch (DocumentException var14) {
            var14.printStackTrace();
            return list;
        }
    }
    
    public static Map<String, String> readProperty(String xmlPath) {
        HashMap map = new HashMap();
        
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(xmlPath + "\\db.xml");
            Element dbe = doc.getRootElement();
            List<Element> elist = dbe.elements();
            Iterator var7 = elist.iterator();
            
            while(var7.hasNext()) {
                Element e = (Element)var7.next();
                if (e.getName().equals("property")) {
                    map.put(e.attributeValue("name"), e.getText());
                }
            }
        } catch (DocumentException var8) {
            var8.printStackTrace();
        }
        
        return map;
    }
    
    private static void writeXml(String outPath, Document doc) {
        try {
            String xmlFileName = "db.xml";
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter writer = null;
            File file = new File(outPath + "/" + xmlFileName);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            
            writer = new XMLWriter(new FileWriter(file), format);
            writer.write(doc);
            writer.close();
        } catch (IOException var6) {
        }
        
    }
}
